package homework20240826;

import homework20240826.dao.IJdbcDao;
import homework20240826.dao.Impl.JdbcDaoImpl;
import homework20240826.pojo.Jdbc;

import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Jdbc jdbc = new Jdbc();
        while (true) {
            System.out.println("请输入操作，1插入数据，2删除数据，3修改数据，4查询数据，5转账");
            Scanner scanner = new Scanner(System.in);

            switch (scanner.nextInt()) {

                case 1: {
                    System.out.println("请输入姓名");
                    jdbc.setName(scanner.next());
                    System.out.println("请输入密码");
                    jdbc.setPwd(scanner.next());
                    System.out.println("请输入邮箱");
                    jdbc.setEmail(scanner.next());
                    System.out.println("请输入账户余额");
                    jdbc.setMoney(scanner.nextInt());
                    System.out.println("请输入生日");
                    String birthday = scanner.next();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        // 将字符串解析为`java.util.Date`
                        java.util.Date parsedDate = dateFormat.parse(birthday);
                        // 将`java.util.Date`转换为`java.sql.Date`
                        java.sql.Date sqlDate = new java.sql.Date(parsedDate.getTime());
                        // 设置到JDBC中
                        jdbc.setBirthday(sqlDate);
                    } catch (ParseException e) {
                        System.out.println("日期格式错误，请输入正确的格式 (yyyy-MM-dd)");
                    }
                    IJdbcDao jdbcDao = new JdbcDaoImpl();
                    if (jdbcDao.insertJdbc(jdbc) != 0) {
                        System.out.println("插入成功");
                    } else System.out.println("插入失败，数据可能重复");
                    break;
                }
                case 2: {
                    System.out.println("请输入要删除的名字");
                    jdbc.setName(scanner.next());
                    IJdbcDao jdbcDao = new JdbcDaoImpl();
                    if (jdbcDao.deleteJdbc(jdbc) != 0) {
                        System.out.println("删除成功");
                    } else System.out.println("删除失败，数据可能不存在");

                    break;
                }
                case 3: {
                    System.out.println("请输入生日被修改成系统时间的人的姓名");
                    jdbc.setName(scanner.next());
                    IJdbcDao jdbcDao = new JdbcDaoImpl();
                    jdbcDao.updateJdbc(jdbc);

                    if (jdbcDao.updateJdbc(jdbc) != 0) {
                        System.out.println("修改成功");
                    } else System.out.println("修改失败，数据可能不存在");

                    break;
                }
                case 4: {
                    System.out.println("正在查询1987以后的用户信息");

                    IJdbcDao jdbcDao = new JdbcDaoImpl();
                    ResultSet rs = jdbcDao.selectJdbc(jdbc);
                    try {
                        while (rs.next()) {
                            jdbc.setName(rs.getString("name"));
                            jdbc.setBirthday(rs.getDate("birthday"));
                            jdbc.setPwd(rs.getString("pwd"));
                            jdbc.setEmail(rs.getString("email"));
                            jdbc.setMoney(rs.getInt("money"));
                            System.out.println(jdbc);

                        }
                        if (jdbc.getName() == null) {
                            System.out.println("未找到任何记录。");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                    break;

                }
                case 5: {
                    System.out.println("请输入转账人姓名");
                    String name1 = scanner.next();
                    System.out.println("请输入被转账人姓名");
                    String name2 = scanner.next();
                    System.out.println("请输入转账金额");
                    int money=scanner.nextInt();
                    IJdbcDao jdbcDao = new JdbcDaoImpl();
                    jdbcDao.Transfer(name1,name2,money);
                }
                default: {
                    System.out.println("程序结束");
                    return;
                }
            }


        }


    }
}
